<template>
  <div class="app-container">
    <!-- 筛选栏 -->
    <div class="filter-container">
      <!-- 输入框 -->
      <el-input
        v-model="filters.groupCode"
        placeholder="组编码"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="filters.groupName"
        placeholder="组名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="filters.paramCode"
        placeholder="参数编码"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="filters.paramName"
        placeholder="参数名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <!-- 按钮 -->
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        添加
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-refresh"
        @click="handleLoadAllDataDict2Cache"
      >
        刷新缓存
      </el-button>
    </div>

    <!-- 数据Table -->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <!-- 表格字段 -->
      <el-table-column label="组名称" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.groupName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="组编码" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.groupCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参数名称" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.paramName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参数编码" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.paramCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参数值" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.paramValue }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.remark }}</span>
        </template>
      </el-table-column>

      <!-- 操作按钮 -->
      <el-table-column
        label="操作"
        align="center"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row, $index }">
          <!-- 编辑 -->
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <!-- 详情 -->
          <el-button size="mini" @click="handleInfo(row)">
            详情
          </el-button>
          <!-- 删除 -->
          <el-button
            v-if="row.status != 'deleted'"
            size="mini"
            type="danger"
            @click="handleDelete(row, $index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <!-- 编辑弹窗 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        :disabled="showInfo"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="组编码" prop="groupCode">
          <el-input v-model="temp.groupCode" placeholder="请输入组编码" />
        </el-form-item>
        <el-form-item label="组名称" prop="groupName">
          <el-input v-model="temp.groupName" placeholder="请输入组名称" />
        </el-form-item>
        <el-form-item label="参数编码" prop="paramCode">
          <el-input v-model="temp.paramCode" placeholder="请输入参数编码值" />
        </el-form-item>
        <el-form-item label="参数名称" prop="paramName">
          <el-input v-model="temp.paramName" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="参数值" prop="paramValue">
          <el-input v-model="temp.paramValue" placeholder="请输入参数值" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="temp.remark"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
            placeholder="请输入描述"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-show="!showInfo" @click="dialogFormVisible = false"> 取消 </el-button>
        <el-button
          v-show="!showInfo"
          type="primary"
          @click="dialogStatus === 'create' ? createData() : updateData()"
        >
          确认
        </el-button>
        <el-button v-show="showInfo" type="primary" @click="dialogFormVisible = false"> 关闭 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入API接口函数
import {del, pageList, save, loadAllDataDict2Cache} from '@/api/video/dataDict'
// 分页
import Pagination from '@/components/Pagination'

export default {
  name: 'DataDict',
  components: { Pagination },
  data() {
    return {
      // 表格数据
      list: null,
      // 数据总数
      total: 0,
      // 表格加载中
      listLoading: false,
      listQuery: {
        // 当前页码
        pageNo: 1,
        // 数据条数
        pageSize: 20,
        // 参数
        params: undefined
      },
      filters: {},
      // 编辑时的模板数据
      temp: {
        id: undefined,
        remark: '',
        status: '1'
      },
      // 编辑弹窗状态：create | update | info
      dialogStatus: '',
      // 显示编辑弹窗
      dialogFormVisible: false,
      // 编辑弹窗-标题
      textMap: {
        update: '编辑',
        create: '添加',
        info: '详情'
      },
      // 编辑弹窗-表单校验规则
      rules: {
        timestamp: [
          {
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // 展示详情
    showInfo() {
      return this.dialogStatus === 'info'
    }
  },
  created() {
    // 默认加载查询一次表格数据
    this.getList()
  },
  methods: {
    // 处理搜索
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置模板数据
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    // 处理添加
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 添加数据
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          save(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    // 处理编辑
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // 修改数据前 先copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // 转时间戳
          save(tempData).then(() => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '编辑成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    // 查看详情
    handleInfo(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'info'
      this.dialogFormVisible = true
    },
    // 处理删除
    handleDelete(row, index) {
      del({id: row.id}).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.list.splice(index, 1)
      })
    },
    // 获取表格数据
    getList() {
      this.listLoading = true
      this.listQuery.filters = this.filters
      pageList(this.listQuery)
        .then((response) => {
          this.list = response.data.list
          this.total = response.data.totalRow
          // 模拟请求时间
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        })
        .catch((_) => {
          this.listLoading = false
        })
    },
    // 刷新缓存
    handleLoadAllDataDict2Cache() {
      loadAllDataDict2Cache().then(() => {
        this.$notify({
          title: '成功',
          message: '刷新成功',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>

<style scoped lang="scss"></style>
